﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class product_insurance_user_insurance_privateHospitalClaim : System.Web.UI.Page
{

    private ClaimBLL bll = new ClaimBLL();

    private const String FILE_PATH = "~/upload/insurance/";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_claim_privateHospital_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid) return;
        String docPath1 =  fileUpload(fu_p1);
        String docPath2 =  fileUpload(fu_p2);
        String docPath3 =  fileUpload(fu_p3);
        String docPath4 =  fileUpload(fu_p4);

        if (!Util.IsBlank(docPath1))
        {
            docPath1 = FILE_PATH + fileUpload(fu_p1);
        }
        if (!Util.IsBlank(docPath2))
        {
            docPath2 = FILE_PATH + fileUpload(fu_p2);
        }
        if (!Util.IsBlank(docPath3))
        {
            docPath3 = FILE_PATH + fileUpload(fu_p3);
        }
        if (!Util.IsBlank(docPath4))
        {
            docPath4 = FILE_PATH + fileUpload(fu_p4);
        }

        string userId = Session["userId"] as String;
        int insuredID = int.Parse(Request.QueryString["certificateNo"]);
        String insurance_acc = new AccountsManager().getInsuranceAccountNo(userId, "H");


        bll.newClaim(insurance_acc, 
                    "", 
                    "PENDING", 
                    0, 
                    DateTime.Now, 
                    "", 
                    insuredID, 
                    "Reinbursement Claim Form", 
                    docPath1,
                    "Medical Certificate", 
                    docPath2,
                    "Refferal Letter", 
                    docPath3, 
                    "Final Summary Bill", 
                    docPath4, 
                    "",
                    "",
                    "", 
                    "");
        this.Response.Redirect("./insurance_after_submit.aspx");
    }
    private String getFileNamePrefix()
    {
        string userId = Session["userId"] as String;
        int caseID = (new AccountsManager().getMaxValue("CLAIM", "caseID")) + 1;
        String fileName = userId + "_" + caseID + "_";


        return fileName;
    }



    private String fileUpload(FileUpload fileUpload)
    {
        string filename = "";
        if (fileUpload.HasFile)
        {
            try
            {
                filename = getFileNamePrefix() + fileUpload.FileName;
                fileUpload.SaveAs(Server.MapPath(FILE_PATH) + filename);

            }
            catch (Exception ex)
            {
                //TODO StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }

        return filename;
    }
    protected void vd_File_ServerValidate(object source, ServerValidateEventArgs args)
    {
        String docPath1 =  fileUpload(fu_p1);
        String docPath2 =  fileUpload(fu_p2);
        String docPath3 =  fileUpload(fu_p3);
        String docPath4 =  fileUpload(fu_p4);


        if (fu_p1.HasFile &&
            fu_p2.HasFile &&
            fu_p3.HasFile &&
            fu_p4.HasFile 
           
            )
        {
            args.IsValid = false;

        }
        else
        {
            args.IsValid = true;
        }
    }
}